/**ai出牌 */
var PkCardTool = require('../../FindTool/PkCardTool');
var _ = require('../../../FrameWork/we_underscore');
var FindSameCardValueTool = require('../../FindTool/FindSameCardValueTool');
var EumPkGroup = require('../../Enum/EumPkGroup');
var AIGetPairs = module.exports = {};

AIGetPairs.doMore = function(_lastCardInfo, _cardsInfo) {
    var resultMore = {
        isFind: false,
        allGetCardsGroup: []
    };
    for (var i = 2; i <= 4; ++i) {
        var result = this.do(_lastCardInfo, _cardsInfo, i);
        if (result.isFind) {
            _.each(result.allGetCardsGroup, function(_cardsInfoTmp) {
                if (_.size(_cardsInfoTmp) == 2) {
                    resultMore.allGetCardsGroup.push(_cardsInfoTmp);
                } else {
                    resultMore.allGetCardsGroup.push([_cardsInfoTmp[0], _cardsInfoTmp[1]]);
                }
            });
        }
    }
    resultMore.isFind = _.size(resultMore.allGetCardsGroup) > 0;
    return resultMore;
};
/***AI出牌
 * _cardsInfo:[1,2,3,6];
 * _number:同样value的数量默认2
 */
AIGetPairs.do = function(_lastCardInfo, _cardsInfo, _number) {
    _number = null == _number ? 2 : _number;
    var cardCnt = _.size(_cardsInfo);
    if (cardCnt < 2) {
        return {
            isFind: false
        };
    }
    var needGetCardInfo = _lastCardInfo.cardsInfo[0];
    var needGetValue = needGetCardInfo.value;
    var findCards = _.filter(_cardsInfo, function(_cardInfo) {
        return needGetValue < _cardInfo.value;
    });
    if (_.size(findCards) < 2) {
        return {
            isFind: false
        };
    }
    var sameCardsList = FindSameCardValueTool.check(findCards);

    /**直接找对子 */
    var canGetList = _.filter(sameCardsList, function(_cards) {
        return _.size(_cards) == _number;
    });
    if (_.size(canGetList) > 0) {
        var choiceCardsInfo = canGetList[0];
        return {
            isFind: true,
            type: EumPkGroup.PAIRS,
            recommendCards: _.first(choiceCardsInfo, 2), //推荐组
            allGetCardsGroup: canGetList //所有组 
        };
    }
    return {
        isFind: false
    };
};